package leetcode_core.leetcode_1;

import java.util.ArrayList;
import java.util.List;

public class Permute {
    private List<List<Integer>> ans = new ArrayList<List<Integer>>();

    public List<List<Integer>> permute(int[] nums) {
        List<Integer> track = new ArrayList<>();
        backtrack(track,nums);
        return ans;
    }
    //(路径,选择列表)
    private void backtrack(List<Integer> track,int[] nums){
        //到达叶子节点
        if(track.size() == nums.length){
            ans.add(new ArrayList<>(track));
        }
        //做选择
        for(int i = 0;i<nums.length;i++){
            if(track.contains(nums[i])){
                continue;
            }
            //做选择
            track.add(nums[i]);
            //回溯递归
            backtrack(track,nums);
            //撤销选择
            track.remove(track.size()-1);
        }
    }

}
